﻿myApp.controller("RegisterCtrl", ['$scope', '$post', '$http', '$routeParams', '$restUrls', '$rootScope', '$location', function ($scope, $post, $http, $routeParams, $restUrls, $rootScope, $location) {
    
    $('input').on('click', function (e) {
        var $this = $(this);
        e.preventDefault();
        setTimeout(function () {
            $(window).scrollTop($this.offset().top - 10);
        }, 200)
    })
    $scope.islogin = 1;
    $scope.sendCountDown = 0;
    $scope.sending = false;
    $scope.logining = false;
    var userinfo = {};
    if ($routeParams.sort) {
        $scope.islogin = $routeParams.sort;
    }

    if ($scope.getUrlParam('userinfo')) {
        userinfo = angular.fromJson($scope.getUrlParam('userinfo').replace(/#[^\?]*/g, ''));
    }

    var msgCountDown = function () {
        if ($scope.sendCountDown <= 0) {
            $('#sendCodeBtn').html('发送验证码');
            $scope.sendCountDown = 0;
        }
        else {
            $scope.sendCountDown--;
            $('#sendCodeBtn').html('验证码(' + $scope.sendCountDown + 'S)');
            setTimeout(function () { msgCountDown(); }, 1000);
        }
    }

    $scope.SendMsg = function (phone) {
        if ($scope.sending) {
            return;
        }

        if ($scope.sendCountDown > 0) {
            return;
        }
        var regex  = /^[1]\d{10}$/;
        if (!regex.test(phone)) {
            $.scojs_message("请输入正确的手机格式", $.scojs_message.TYPE_ERROR);
            return;
        }
        $scope.sending = true;
        $http({ method: "GET", url: $restUrls["phoneValidate"], params: { phone: phone } }).success(function (result) {
            if (result.success == 1) {
                $scope.sendCountDown = 60;
                setTimeout(function () { msgCountDown(); }, 0);
                $scope.sending = false;
                $.scojs_message('验证码发送成功', $.scojs_message.TYPE_OK);
            }
            else {
                $scope.sendCountDown = 0;
                setTimeout(function () { msgCountDown(); }, 0);
                $.scojs_message(result.data.message, $.scojs_message.TYPE_ERROR);
                $scope.sending = false;
            }
        }).error(function (data, status, headers, config) {
            $scope.sending = false;
        });
    }

   
    $scope.registering = false;
    $scope.Regitster = function (register) {
        if ($scope.registering) {
            return;
        }
        if (!$scope.RegisterForm.$valid) {
            $scope.showError = true;
            return;
        }
        var parameters = {
            telephone: register.telephone,
            code: register.code,
            password: register.password,
            firstname: userinfo.name,
            image: userinfo.image,
            open_id: userinfo.open_id
        };
        $scope.registering = true;
        $post($restUrls["register"], parameters).success(function (result) {
            if (result.success == 1) {
                $scope.registering = false;
                window.location.href = "#/step";
            }
            else {
                $scope.registering = false;
                $.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
            }
        }).error(function (data, status, headers, config) {
            $scope.registering = false;
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        })
    }
}]);
